/*
 * Hidden Equity LLC
 * Author: Maksym Fedyay, Kyiv, mcsimf@gmail.com
 * Date: 29.02.2012
 */

package com.hiddenequity.adextensions_service;

import java.util.ArrayList;

import com.hiddenequity.acts.Act_Super;
import com.hiddenequity.utils.SOAPUtils;
import com.hiddenequity.utils.UrlBuncher;

public class CampaignAdExtensionService {

	public static final String URL_CAMPAIGN_AD_EXTENSION_SERVICE = UrlBuncher.URL_BASE + "CampaignAdExtensionService";
	public static final String LOCATION_EXTENSION = "LocationExtension";
	public static final String SITE_LIKS_EXTENSION = "SitelinksExtension";
	
	
	
	/*
	 * Get location extension list
	 */
	public static String getCampaignAdExtensionList(ArrayList<String> dateRange, int status){
		String SOAPBody =
				"<soap:Body>" +
					"<get>" +
						"<serviceSelector>" +
							"<fields>AdExtensionId</fields>" +
							"<fields>Status</fields>" +
							"<fields>Address</fields>" +
							"<fields>DisplayText</fields>" +
							"<fields>DestinationUrl</fields>" +
							"<fields>Clicks</fields>" +
							"<fields>Ctr</fields>" +
							"<fields>Cost</fields>" +
							"<fields>Conversions</fields>" +
							"<fields>Impressions</fields>";

		if(status != 0){
			String statusName = "";
			
			switch (status) {
				case Act_Super.STATUS_ACTIVE:
					statusName = "ACTIVE";
					break;
				case Act_Super.STATUS_PAUSED: //TODO: Do something with global status changing
					statusName = "DELETED";
					break;
//				case Act_Super.STATUS_DELETED:
//					statusName = "DELETED";
//					break;
			}
			
				SOAPBody = SOAPBody + 
					"<predicates>" +
						"<field>Status</field>" +
						"<operator>EQUALS</operator>" +
						"<values>" + statusName + "</values>" +
					"</predicates>";
		}
		
		if(!dateRange.get(0).equals("")){
			SOAPBody = SOAPBody +	"<dateRange>" +
						"<min>" + dateRange.get(0) +"</min>" +
						"<max>" + dateRange.get(1) +"</max>" +
						"</dateRange>";
		}
							
		SOAPBody = SOAPBody + 
							"<ordering>" +
								"<field>Clicks</field>" +
								"<sortOrder>DESCENDING</sortOrder>" +
							"</ordering>" +
						"</serviceSelector>" +
					"</get>" +
				"</soap:Body>";		

		String envelope = SOAPUtils.buildEnvelope(SOAPBody);
		return envelope;
	}//getLocationExtensionList
	
	
	
	/*
	 * Remove location extension
	 */
	static public String removeLocationAdExtension(String extensionId, String campaignId){
		String SOAPBody = 
				"<soap:Body>" +
						"<mutate xmlns=" + SOAPUtils.XMLNS +
						" xmlns:xsi="+ SOAPUtils.XMLNSXSI  +">" +
						"<operations>" +
							"<operator>REMOVE</operator>" +
							"<operand xsi:type='CampaignAdExtension'>" +
								"<campaignId>" + campaignId + "</campaignId>" +
								"<adExtension xsi:type='LocationExtension'>" + 
									"<id>" + extensionId + "</id>" +
									//"<status>DELETED</status>" +
								"</adExtension>" +
							"</operand>" +
						"</operations>" +
					"</mutate>" +
				"</soap:Body>";		
		String envelope = SOAPUtils.buildEnvelope(SOAPBody);
		return envelope;
		//return null;
	}
	
	
	/*
	 * Set location extension status
	 */
	static public String setLocationAdExtensionStatus(String locationExtensionId, String campaignId, int status){
		
		if(Act_Super.STATUS_DELETED != status)
			return null;
		
		return removeLocationAdExtension(locationExtensionId, campaignId);
	}//setLocationExtensionStatus

	
	
	/*
	 * Location extension add set operations
	 */
	public static String locationExtensionOperation(String campaignId, String countryCode, String companyName,
			String phoneNumber, String streetAddress, String streetAddress2,
			String cityName, String provinceName, String postalCode, String latitudeInMicroDegrees, 
			String longitudeInMicroDegrees, String encodedLocation) {

		
		String streetAddressTag = "";
		if(!streetAddress.equals("") && streetAddress != null)
			streetAddressTag = "<streetAddress>" + streetAddress +"</streetAddress>";
	
		String streetAddress2Tag = "";
		if(!streetAddress2.equals("") && streetAddress2 != null)
			streetAddress2Tag = "<streetAddress2>" + streetAddress2 + "</streetAddress2>";
		
		String cityNameTag = "";
		if(!cityName.equals("") && cityName != null)
			cityNameTag = "<cityName>" + cityName + "</cityName>";
		
		String postalCodeTag = "";
		if(!postalCode.equals("") && postalCode != null)
			postalCodeTag = "<postalCode>" + postalCode + "</postalCode>";
		
		String countryCodeTag = "";
		if(!countryCode.equals("") && countryCode != null)
			countryCodeTag = "<countryCode>" + countryCode + "</countryCode>";
		
		String provinceNameTag = "";
		if(!provinceName.equals("") && provinceName != null)
			provinceNameTag = "<provinceName>" + provinceName + "</provinceName>";
		
		
		String SOAPBody = 
				"<soap:Body>" +
						"<mutate xmlns=" + SOAPUtils.XMLNS +
						" xmlns:xsi="+ SOAPUtils.XMLNSXSI  +">" +
						"<operations>" +
							"<operator>ADD</operator>" +
							"<operand xsi:type='CampaignAdExtension'>" +
								"<campaignId>" + campaignId + "</campaignId>" +
								"<adExtension xsi:type='LocationExtension'>" + 
									"<address>" +
										streetAddressTag +
										streetAddress2Tag + 
										cityNameTag + 
										provinceNameTag +
										postalCodeTag +
										countryCodeTag +
									"</address>" +
									"<geoPoint>" +
										"<latitudeInMicroDegrees>" + latitudeInMicroDegrees + "</latitudeInMicroDegrees>" +
										"<longitudeInMicroDegrees>" + longitudeInMicroDegrees + "</longitudeInMicroDegrees>" +
									"</geoPoint>" +
									"<encodedLocation>" + encodedLocation +"</encodedLocation>" +
									"<companyName>" + companyName + "</companyName>" +
									"<phoneNumber>" + phoneNumber + "</phoneNumber>" +
									"<source>ADWORDS_FRONTEND</source>" +
								"</adExtension>" +
							"</operand>" +
						"</operations>" +
					"</mutate>" +
				"</soap:Body>";		
		
		String envelope = SOAPUtils.buildEnvelope(SOAPBody);
		return envelope;
	}//locationExtensionOperation
	
	
	
	/*
	 * Site links extension add set operation
	 */
	public static String sitelinkExtensionOperation(String campaignId, String[] displayText, String[] destinationUrl){
		
		String linksTag = null;
		
		for(int i=0; i<10; i++){
			if(displayText[i] != null && destinationUrl != null){
				linksTag += 
				"<sitelinks>" +
					"<displayText>" + displayText[i] + "</displayText>" +
					"<destinationUrl>" + destinationUrl[i] + "</destinationUrl>" +
				"</sitelinks>";	
			}
		}

		String SOAPBody = 
				"<soap:Body>" +
						"<mutate xmlns=" + SOAPUtils.XMLNS +
						" xmlns:xsi="+ SOAPUtils.XMLNSXSI  +">" +
						"<operations>" +
							"<operator>ADD</operator>" +
							"<operand xsi:type='CampaignAdExtension'>" +
								"<campaignId>" + campaignId + "</campaignId>" +
								"<adExtension xsi:type='SitelinksExtension'>" + 
									linksTag +
								"</adExtension>" +
							"</operand>" +
						"</operations>" +
					"</mutate>" +
				"</soap:Body>";		
		
		String envelope = SOAPUtils.buildEnvelope(SOAPBody);
		return envelope;		
	}//sitelinkExtensionOperation

	

	/*
	 * 
	 */
	public static String setSiteLinkAdExtensionStatus(String extensionId, String campaignId, int status, 
			String[] displayText, String[] destinationUrl) {
		if(Act_Super.STATUS_DELETED != status)
			return null;
		
		return removeSiteLinkAdExtension(extensionId, campaignId, displayText, destinationUrl);
	}//setSiteLinkAdExtensionStatus


	
	/*
	 * 
	 */
	public static String removeSiteLinkAdExtension(String extensionId,
			String campaignId, String[] displayText, String[] destinationUrl) {
		
		String linksTag = "";
		
		for(int i=0; i<10; i++){
			if(displayText[i] != null){
				linksTag += 
				"<sitelinks>" +
					"<displayText>" + displayText[i] + "</displayText>" +
					"<destinationUrl>" + destinationUrl[i] + "</destinationUrl>" +
				"</sitelinks>";	
			}
		}
		
		String SOAPBody = 
				"<soap:Body>" +
					"<mutate xmlns=" + SOAPUtils.XMLNS +
						" xmlns:xsi="+ SOAPUtils.XMLNSXSI  +">" +
						"<operations>" +
							"<operator>REMOVE</operator>" +
							"<operand xsi:type='CampaignAdExtension'>" +
								"<campaignId>" + campaignId + "</campaignId>" +
								"<adExtension xsi:type='SitelinksExtension'>" + 
									"<id>" + extensionId + "</id>" +
									linksTag +
								"</adExtension>" +
							"</operand>" +
						"</operations>" +
					"</mutate>" +
				"</soap:Body>";		
		String envelope = SOAPUtils.buildEnvelope(SOAPBody);
		return envelope;
	}//removeSiteLinkAdExtension
	
	
}
